// components/filter/filter.js
Component({
  externalClasses: ['custom-class'],

  /**
   * 组件的属性列表
   */
  properties: {
    // 弹出弹层标识
    flag: {
      type: Boolean,
      value: false,
      observer(newVal, oldVal) {
        this.setData({
          show: true,
        })
      }
    },
    // 模块名称
    table: {
      type: String,
      value: '',
    },
    // 筛选条件
    searchList: {
      type: Array,
      value: [],
    },
    // 是否格式查询筛选值
    fmt: {
      type: Boolean,
      value: true,
    },
    // 传入条件值
    filterObj: {
      type: Object,
      value: {}
    },
  },

  // 监听
  observers: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    // searchList: [
    //   { field_name: '账号', type: 'reference1', field_id: 'zhanghao', optionList: [
    //     { id: '001', name: '中国银行', },
    //     { id: '001', name: '支付宝', },
    //     { id: '001', name: '微信', },
    //     { id: '001', name: '京东支付', },
    //   ], optionId: 'id', optionName: 'name', },
    //   { field_name: '收入类型', type: 'picklist', field_id: 'leixing', optionList: [
    //     { label: '收入', },
    //     { label: '支出', },
    //   ], },
    //   { field_name: '科目', type: 'multiple', field_id: 'bbc', optionList: [
    //     { label: '语文', },
    //     { label: '数学', },
    //     { label: '英语', },
    //     { label: '物理', },
    //     { label: '化学', },
    //     { label: '生物', },
    //   ], },
    //   { field_name: '科目名称', type: 'reference', field_id: 'kemu', optionList: [
    //     { id: '000', name: '语文', },
    //     { id: '001', name: '数学', },
    //     { id: '002', name: '政治', },
    //     { id: '003', name: '英语', },
    //     { id: '004', name: '物理', },
    //     { id: '005', name: '化学', },
    //     { id: '006', name: '生物', },
    //   ], },
    //   { field_name: '发生日期', type: 'date', field_id: 'fashengriqi', },
    //   { field_name: '发生日期', type: 'datetime', field_id: 'fashengriqi1', },
    //   { field_name: '文本', type: 'text', field_id: 'wenben', },
    // ],

    resetFlag: false,
  },

  // 生命周期
  lifetimes: {
    created() {
      this.query = {}
    },
    attached() {},
    ready() {},
    moved() {},
    detached() {},
    error() {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    filterIptChange(e) {
      let { field_id, val, } = e.detail
      if(val === '') {
        delete this.query[field_id]
      } else {
        this.query[field_id] = e.detail
      }
      // 221018 查询条件更改，触发 change 事件
      this.triggerEvent('change', e.detail)
    },
    // 重置
    reset(e) {
      this.query = {}
      this.setData({
        resetFlag: !this.data.resetFlag,
      })

      // 221018 增加了插槽，触发重置事件
      this.triggerEvent('reset')
    },
    // 查询
    search(e) {
      let { table, fmt, } = this.data,
          searchWhere = []
      if(fmt) {
        if(table) table += '.'
        Object.values(this.query).forEach(item => {
          let { field_id, type, val, } = item,
              obj = {
                CompareValue: '',
                CompareWith: `${table}${field_id}`,
                Operator: '=',
                or: 0,
              }
          if(['picklist', 'text', 'num', 'digit', ].includes(type)) {
            obj.CompareValue = val
            obj.Operator = 'like'
          } else if(type === 'multiple') {
            val.forEach(v => {
              searchWhere.push({
                CompareValue: v,
                CompareWith: `${table}${field_id}`,
                Operator: 'like',
                or: 0,
              })
            })
            return
          } else if(['reference1', 'reference', ].includes(type)) {
            obj.CompareValue = val
          } else if(['datetime', 'date', ].includes(type)) {
            if(typeof val === 'object') {
              Object.entries(val).forEach(([k, v]) => {
                if(!v) return
                searchWhere.push({
                  CompareValue: v,
                  CompareWith: `${table}${field_id}`,
                  Operator: k === 'endDate' ? '<=' : '>=',
                  or: 0,
                })
              })
              return
            } else {
              let table = this.data.table
              if(!table) return
              if(val === 0) { // 今天
                obj.CompareValue = 0
                obj.CompareWith = `DATEDIFF(day,${table}.${field_id},GETDATE())`
              } else if(val === 1) {  // 近7天
                obj.CompareValue = 7
                obj.CompareWith = `DATEDIFF(day,${table}.${field_id},GETDATE())`
                obj.Operator = '<'
              } else if(val === 2) {  // 本月
                obj.CompareValue = 'convert(varchar(6),GETDATE(),112)'
                obj.CompareWith = `convert(varchar(6),${table}.${field_id},112)`
              } else if(val === 3) {  // 本季
                obj.CompareValue = 'datepart(quarter,GETDATE())'
                obj.CompareWith = `datepart(quarter,${table}.${field_id})`
              } else if(val === 4) {  // 本年
                obj.CompareValue = 'datepart(year,GETDATE())'
                obj.CompareWith = `datepart(year,${table}.${field_id})`
              }else { //221126 yan 修改 日期查询条件 没有对应日期时，直接传出值
                obj.CompareValue = val;
                obj.CompareWith = table + '.' + field_id
              }
            }
          } else if(type === 'dateonly') {
            obj.CompareValue = val
          }else if (type === 'bit'){
            obj.CompareValue = val
          }
          searchWhere.push(obj)
        })

        this.triggerEvent('search', searchWhere)
        this.closePopup()
      } else {
        this.triggerEvent('search', this.query)
        this.closePopup()
      }
    },


    // 阻止冒泡
    stopBubble(e) {},
    closePopup() {
      this.setData({
        show: false,
      })
    },
  }
})
